import pymysql


class dbuilt(object):
    @classmethod
    def get_connect(cls):
        connect = pymysql.connect(host='211.103.136.244', port=7061,
                                  user='student', password='iHRM_student_2021',
                                  database='ihrm', charset='utf8')
        return connect

    @classmethod
    def qurery_one(cls, sql):
        connect = cls.get_connect()
        cursor = connect.cursor()
        cursor.execute(sql)
        one = cursor.fetchone()
        cursor.close()
        connect.close()
        return one

    @classmethod
    def qurery_all(cls, sql):
        connect = cls.get_connect()
        cursor = connect.cursor()
        cursor.execute(sql)
        _all = cursor.fetchall()
        cursor.close()
        connect.close()
        return _all

    @classmethod
    def iud_data(cls, sql):
        connect = cls.get_connect()
        cursor = connect.cursor()
        n = None
        try:
            n = cursor.execute(sql)
        except Exception as e:
            print('出现异常,执行回滚,异常为 ', e)
            connect.rollback()
        else:
            print('没有出现异常,提交事务')
            connect.commit()
        finally:
            print('执行结束,关闭游标和连接')
            cursor.close()
            connect.close()
        return n
